﻿using Microsoft.EntityFrameworkCore;
using ST.Buz.Base.Entity;
using ST.Buz.Base.Mapping;
using System.Data.Common;
using System.Data;

namespace ST.Buz.Base
{
	public partial class BuzContext
	{
		#region 实体
		public virtual DbSet<BJG_Machining> Machining { get; set; }
		public virtual DbSet<IMS_Partner> Partner { get; set; }//合作伙伴
		public virtual DbSet<IMS_PartnerOAuth> PartnerOAuth { get; set; }//合作伙伴与OAuth关联信息
		public virtual DbSet<IMS_FinanceRecv> FinanceRecv { get; set; }
		public virtual DbSet<IMS_FinanceRecvDetail> FinanceRecvDetail { get; set; }
		public virtual DbSet<IMS_FinanceRecvDue> FinanceRecvDue { get; set; }

		#endregion

		protected void OnModelCreatingBuz(ModelBuilder modelBuilder)
		{
#if DEBUG
			modelBuilder.ApplyConfiguration(new Map_BJG_Machining());
			modelBuilder.ApplyConfiguration(new Map_IMS_Partner());
			modelBuilder.ApplyConfiguration(new Map_IMS_PartnerOAuth());
			modelBuilder.ApplyConfiguration(new Map_IMS_FinanceRecv());
			modelBuilder.ApplyConfiguration(new Map_IMS_FinanceRecvDetail());
			modelBuilder.ApplyConfiguration(new Map_IMS_FinanceRecvDue());

#endif
		}

		public object ExecuteScalar(string sql, DbParameter[] parameters, CommandType cmdType = CommandType.StoredProcedure)
		{
			object ret = null;
			using (var cmd = Database.GetDbConnection().CreateCommand())
			{
				cmd.CommandText = sql;
				cmd.CommandType = cmdType;
				if (parameters != null && parameters.Length > 0)
				{
					cmd.Parameters.AddRange(parameters);
				}
				Database.OpenConnection();
				ret = cmd.ExecuteScalar();
				Database.CloseConnection();
			}
			return ret;
		}
	}
}
